package com.summer.safe.controller.system;

import com.summer.safe.base.BaseController;
import com.summer.safe.base.Page;
import com.summer.safe.config.annotation.ResultWrapper;
import com.summer.safe.config.result.ResultCode;
import com.summer.safe.system.entity.Log;
import com.summer.safe.system.service.LogService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;

/**
 * 日志Controller
 *
 * @author lm
 * @date 2020-06-16 16:23:23
 */
@Controller
@RequestMapping(value = "/sys/log")
public class LogController extends BaseController {
    private String PREFIX = "/system/log/";

    @Autowired
    private LogService logService;

    @ModelAttribute("log")
    public Log get(@RequestParam(required=false) String id) {
        if (StringUtils.isNotBlank(id)){
            return logService.get(id);
        }else{
            return new Log();
        }
    }

    /**
     * 列表页面的跳转
     *
     * @author lm
     * @date 2020-06-16 16:23:23
     */
    @RequiresPermissions("sys:log:view")
    @GetMapping
    public String index() {
        return PREFIX + "logList.html";
    }

    /**
     * 获取列表数据
     *
     * @author lm
     * @date 2020-06-16 16:23:23
     */
    @GetMapping("/dataList")
    @ResultWrapper(ResultCode.SUCCESS_QUERY)
    public Page list(Log log, HttpServletRequest request) {
        if (log.getBeginDate() == null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DATE, -3);
            log.setBeginDate(calendar.getTime());
            log.setEndDate(new Date());
        }
        Page<Log> page = logService.findPage(new Page(request), log);
        return page;
    }
    /**
     * 表单页面的跳转
     *
     * @author lm
     * @date 2020-06-16 16:23:23
     */
    @RequiresPermissions("sys:log:edit")
    @GetMapping("/form")
    public String form(Log log, Model model) {
        model.addAttribute("log", log);
        return PREFIX + "logForm.html";
    }




    /**
     * 提交数据
     *
     *@author lm
     * @date 2020-06-16 16:23:23
     */
    @PostMapping("/save")
    @ResultWrapper(ResultCode.SUCCESS_SAVE)
    public void save(Log log) {
        logService.save(log);
    }
    /**
     * 删除数据
     *
     *@author lm
     * @date 2020-06-16 16:23:23
     */
    @PostMapping("/delete")
    @ResultWrapper(ResultCode.SUCCESS_DELETE)
    public void delete(Log log) {
        logService.delete(log);
    }
}